const express = require("express");
const app = express();
const http = require("http").Server(app);
const cors = require("cors");
const bodyParser = require("body-parser");
const router = express.Router()

const {
  videoToTs,
  sliceVideoFromFFmpeg,
  getVideoDuration,
} = require("./router/index");


router.get("/cutVideo", async (req, res) => {
  try {
    const videoUrl = "./video/three.mp4"; // 视频文件路径
    const duration = await getVideoDuration(videoUrl); // 获取视频时长
    console.log("视频时长：", duration);
    const outputPath = "./video/chunk"; // 输出目录路径
    const m3u8Path = await sliceVideoFromFFmpeg(videoUrl, outputPath); // 使用 FFmpeg 切割视频为 HLS 格式
    if (m3u8Path) {
      res.send({
        message: "查询视频切片成功",
        m3u8Path,
        code: 200,
      });
    }
  } catch (error) {
    console.error("发生错误：", error);
    res.status(500).send("发生错误");
  }
});
// 将 cors 注册为全局中间件
app.use(cors());
app.use(express.static("./view/friends-both"));
app.use(express.static("./video"));

// 配置中间件
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use('/',router)
// 启动服务器
const port = 3000;
http.listen(port, () => {
  console.log(`http://127.0.0.1:3000`);
});

// 关于socketio的操作 聊天socket
const socket = require("./router/socket");
socket(http);
